package cn.edu.fzu.homemaking.controller;

import cn.edu.fzu.homemaking.common.PageVO;
import cn.edu.fzu.homemaking.domain.entity.User;
import cn.edu.fzu.homemaking.domain.entity.UserDemand;
import cn.edu.fzu.homemaking.domain.enums.DemandStatusEnum;
import cn.edu.fzu.homemaking.domain.vo.UserDemandVo;
import cn.edu.fzu.homemaking.exception.ServiceException;
import cn.edu.fzu.homemaking.service.DemandService;
import cn.edu.fzu.homemaking.util.UserInfoUtil;
import cn.edu.fzu.homemaking.web.Result;
import cn.edu.fzu.homemaking.web.RetCode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * @Author jiebin
 * @Date 2023/4/2
 */
@RestController
@RequestMapping("/api/user")
@Api(tags = "用户需求管理")
public class UserDemandController {

    @Resource
    private DemandService demandService;

    @PostMapping("/auth/demand/insert")
    @ApiOperation(value ="发布用户需求",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "labelId", value = "服务标签", required = true),
            @ApiImplicitParam(name = "demand", value = "需求描述", required = true),
            @ApiImplicitParam(name = "time", value = "约定时间", required = true),
            @ApiImplicitParam(name = "address", value = "用户地址", required = true),
            @ApiImplicitParam(name = "person", value = "人数", required = true),
            @ApiImplicitParam(name = "price", value = "一口价", required = true),
            @ApiImplicitParam(name = "name", value = "联系人姓名", required = true),
            @ApiImplicitParam(name = "phone", value = "联系电话", required = true),
            @ApiImplicitParam(name = "email", value = "邮箱地址", required = true)
    })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result insert(Long labelId, String demand, String time, String address, Integer person,
                         BigDecimal price, String name, String phone, String email, HttpServletRequest request){
        User user = UserInfoUtil.getUserInfo(request,false);
        demandService.insert(user.getId(), labelId, demand, time, address, person, price, name, phone, email, DemandStatusEnum.WAIT);
        return new Result(RetCode.SUCCESS,"发布需求成功");
    }

    @GetMapping("/demand/list")
    @ApiOperation(value = "用户需求列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "labelId", value = "服务标签"),
            @ApiImplicitParam(name = "startTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "address", value = "地区"),
            @ApiImplicitParam(name = "status", value = "需求订单状态"),
            @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true)
    })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result list(Long labelId, String startTime, String endTime, String address, DemandStatusEnum status,
                       Integer pageNum, Integer pageSize, HttpServletRequest request){
        Long userId = null;
        User user = UserInfoUtil.getUserInfo(request, true);
        if(user != null){
            userId = user.getId();
        }
        try(Page<UserDemand> page = PageHelper.startPage(pageNum,pageSize)){
            List<UserDemandVo> list = demandService.list(userId, labelId, startTime, endTime, address, status);
            return new Result(new PageVO<>(page.getTotal(),list));
        }
    }

    @PostMapping("/auth/demand/updateDemandStatus")
    @ApiOperation(value = "更新用户需求状态", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "需求id"),
            @ApiImplicitParam(name = "status", value = "订单状态")
    })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result updateDemandStatus(Long id, DemandStatusEnum status, HttpServletRequest request){
        UserDemand userDemand = demandService.selectById(id);
        if (Objects.equals(DemandStatusEnum.ACCEPT,userDemand.getStatus())){
            throw new ServiceException("需求已被接单，无法修改需求单状态");
        }
        demandService.updateDemandStatus(id, status);
        return Result.SUCCESS;
    }

    @PostMapping("/auth/demand/delete")
    @ApiOperation(value = "根据id删除用户需求", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "需求id",required = true),
    })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result delete(Long id, HttpServletRequest request){
        User user = UserInfoUtil.getUserInfo(request,false);
        UserDemand userDemand = demandService.selectById(id);
        if (Objects.equals(user.getId(),userDemand.getUserId())){
            demandService.delete(id);
            return new Result(RetCode.SUCCESS,"删除需求成功");
        }
        throw new ServiceException("删除需求失败");
    }

    @GetMapping("/demand/selectById")
    @ApiOperation(value = "根据id获取用户需求", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "需求id",required = true)
    })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result selectById(Long id, HttpServletRequest request){
        UserDemand userDemand = demandService.selectById(id);
        return new Result(userDemand);
    }
}
